package com.cyt.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.BaseUtil;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.dao.SysDictContentDao;
import com.cyt.sys.model.po.SysDictContent;
import com.cyt.sys.model.vo.SysDictContentVo;
import com.cyt.sys.model.vo.SysDictViewVo;
import com.cyt.sys.service.SysDictContentService;
import com.cyt.sys.util.cache.CacheUtils;
import com.cyt.sys.util.cache.DictUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *@Author: YuShengQiang
 *@Date: 2018-11-16 11:52
 *@Description:数据字典内容service实现类
 */
@Service("baseDictContentService")
public class SysDictContentServiceImpl extends BaseServiceImpl<SysDictContent> implements SysDictContentService {
    @Resource
    private SysDictContentDao sysDictContentDao;

    @Autowired
    public void setBaseDao(SysDictContentDao sysDictContentDao) {
        this.baseDao = sysDictContentDao;
    }

   /**
    *@Author: YuShengQiang
    *@Date: 2018-11-23 14:17
    *@Description: 分页查询
    */
    public ReturnResult page(SysDictContentVo sysDictContentVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" sort desc,create_time desc");
        List<SysDictContent> list=selectList(sysDictContentVo);
        PageInfo<SysDictContent> page=new PageInfo<SysDictContent>(list);
        return new ReturnResult(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     *@Author: YuShengQiang
     *@Date: 2018-11-24 10:50
     *@Description:列表查询
     */
    public ReturnResult list(SysDictContentVo sysDictContentVo){
        return new ReturnResult(selectList(sysDictContentVo));
    }
    /**
     *@Author: YuShengQiang
     *@Date: 2018-11-23 14:19
     *@Description: 根据dictType获取集合,多个以“，”隔开
     */
    public ReturnResult listByCode(String dictTypes){
        if(StrUtil.isEmpty(dictTypes)){
            return new ReturnResult(ErrorCode.CODE_201, ErrorCode.CODE_201_MSG);
        }
        String[] dictTyps =dictTypes.split(",");
        List<SysDictContent> lists=new ArrayList<SysDictContent>();
        for(String dicttype :dictTyps){
            SysDictViewVo sysDictViewVo = DictUtils.getBaseDictViewVo(dicttype);
            if(sysDictViewVo ==null){
                return new ReturnResult(ErrorCode.CODE_201,dicttype+ErrorCode.CODE_201_MSG);
            }
            List<SysDictContent> list= sysDictViewVo.getSysDictContentList();
            lists.addAll(list);
        }
        if (lists != null) {
            return new ReturnResult(lists);
        }
        else{
            return new ReturnResult(ErrorCode.CODE_201, ErrorCode.CODE_201_MSG);
        }

    }

    /**
     *@Author: YuShengQiang
     *@Date: 2018-11-26 10:37
     *@Description: 查询
     */
    private List<SysDictContent> selectList(SysDictContentVo sysDictContentVo){
        Example example = new Example(SysDictContent.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause(" sort desc,create_time desc");
        if(StringUtil.isNotEmpty(sysDictContentVo.getDictType())){
            criteria.andEqualTo("dictType", sysDictContentVo.getDictType());
        }
        if(StringUtil.isNotEmpty(sysDictContentVo.getRemoveTag())){
            criteria.andEqualTo("removeTag", sysDictContentVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(sysDictContentVo.getValue())){
            criteria.andEqualTo("value", sysDictContentVo.getValue());
        }
        if(StringUtil.isNotEmpty(sysDictContentVo.getName())){
            criteria.andLike("name","%"+ sysDictContentVo.getName()+"%");
        }
        if(StringUtil.isNotEmpty(sysDictContentVo.getStatus())){
            criteria.andEqualTo("status", sysDictContentVo.getStatus());
        }
        List<SysDictContent> list= sysDictContentDao.selectByExample(example);
        return list;
    }

    /**
     * @auther: liufu zhu
     * @create: 2018/11/29 16:15
     * @Description:  保存/更新字典数据
     */
    @Override
    public ReturnResult addOrUpdate(SysDictContentVo sysDictContentVo) {
        if (sysDictContentVo.getId() == null || "".equals(sysDictContentVo.getId())) {
            SysDictContent sysDictContent = new SysDictContent();
            sysDictContent.setId(BaseUtil.uId());
            sysDictContent.setValue(sysDictContentVo.getValue());
            sysDictContent.setName(sysDictContentVo.getName());
            sysDictContent.setSort(sysDictContentVo.getSort());
            sysDictContent.setStatus(sysDictContentVo.getStatus());
            sysDictContent.setDictType(sysDictContentVo.getDictType());
            sysDictContent.setRemark(sysDictContentVo.getRemark());
            sysDictContent.setCreateTime(new Date());
            sysDictContent.setRemoveTag(BaseConstant.REMOVE_TAG_NO);
            int id = sysDictContentDao.insert(sysDictContent);
            if (id <= 0) {
                return new ReturnResult(BaseConstant.DATABASE_ERROR_INSERT);
            }
        } else {
            SysDictContent sysDictContent = sysDictContentDao.selectByPrimaryKey(sysDictContentVo.getId());
            sysDictContent.setValue(sysDictContentVo.getValue());
            sysDictContent.setName(sysDictContentVo.getName());
            sysDictContent.setSort(sysDictContentVo.getSort());
            sysDictContent.setStatus(sysDictContentVo.getStatus());
            sysDictContent.setDictType(sysDictContentVo.getDictType());
            sysDictContent.setRemark(sysDictContentVo.getRemark());
            sysDictContent.setCreateTime(new Date());
            sysDictContent.setRemoveTag(BaseConstant.REMOVE_TAG_NO);
            int id = sysDictContentDao.updateByPrimaryKey(sysDictContent);
            if (id <= 0) {
                return new ReturnResult(BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
        CacheUtils.remove(DictUtils.CACHE_DICT_MAP);
        return new ReturnResult(BaseConstant.DATABASE_SUCCEED_MSG);
    }

    /**
     * @auther: liufu zhu
     * @create: 2018/11/29 16:41
     * @Description:  删除字典数据
     */
    @Override
    public ReturnResult delete(String ids) {
        String[] idList=ids.split(",");
        boolean flage=true;
        //StringBuffer stringBuffer=new StringBuffer();
        for (String id:idList){
            SysDictContent sysDictContent = sysDictContentDao.selectByPrimaryKey(id);
            sysDictContent.setRemoveTag(BaseConstant.REMOVE_TAG_YES);
            int num= sysDictContentDao.updateByPrimaryKey(sysDictContent);
            if(num>0){
                flage=true;
            }else{
                //stringBuffer.append("");
            }
        }
        if(flage){
            CacheUtils.remove(DictUtils.CACHE_DICT_MAP);
            return new ReturnResult();
        }else{
            CacheUtils.remove(DictUtils.CACHE_DICT_MAP);
            return new ReturnResult(ErrorCode.CODE_101_MSG);
        }
    }

    /**
     * @auther: liufu zhu
     * @create: 2018/11/30 9:38
     * @Description:  根据dictType删除
     */
    @Override
    public ReturnResult updateByDictType(String dictType){
        sysDictContentDao.updateByDictType(dictType);
        CacheUtils.remove(DictUtils.CACHE_DICT_MAP);
        return new ReturnResult(ErrorCode.CODE_0_MSG);
    }

    /**
     *@Author: YuShengQiang
     *@Date: 2018-11-26 10:37
     *@Description: 查询
     */
    public List<SysDictContent> selectList1(SysDictContentVo sysDictContentVo){
        Example example = new Example(SysDictContent.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause(" sort desc,create_time desc");
        if(StringUtil.isNotEmpty(sysDictContentVo.getDictType())){
            criteria.andEqualTo("dictType", sysDictContentVo.getDictType());
        }
        if(StringUtil.isNotEmpty(sysDictContentVo.getStatus())){
            criteria.andEqualTo("status", sysDictContentVo.getStatus());
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        List<SysDictContent> list= sysDictContentDao.selectByExample(example);
        return list;
    }

}
